package com.jay.base.lock;

import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by caoyuan7 on 2019/1/16.
 */
public class RenntTrantLockTest {
    private ArrayList<Integer> arrayList = new ArrayList<Integer>();
    private Lock lock = new ReentrantLock(true);

    public static void main(String args[]){
        final RenntTrantLockTest test = new RenntTrantLockTest();
        MyThread thread1 = new MyThread(test);
        MyThread thread2 = new MyThread(test);
        thread1.start();
        thread2.start();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread2.interrupt();

    }
    public void insert(Thread thread) throws InterruptedException{

        lock.lockInterruptibly();   //注意，如果需要正确中断等待锁的线程，必须将获取锁放在外面，然后将InterruptedException抛出
        try {
            System.out.println(thread.getName()+"得到了锁");
            long startTime = System.currentTimeMillis();
            for(    ;     ;) {
                if(System.currentTimeMillis() - startTime >= Integer.MAX_VALUE)
                    break;
                //插入数据
            }
        }catch (Exception e){

        }finally {
            System.out.println(Thread.currentThread().getName()+"执行finally");
            lock.unlock();
            System.out.println(thread.getName()+"释放了锁");
        }
    }
}
class MyThread extends Thread{
    private RenntTrantLockTest test = null;
    public MyThread(RenntTrantLockTest test){
        this.test= test;
    }
    public void run(){
        try {
            test.insert(Thread.currentThread());
        }catch (InterruptedException e){
            System.out.println(Thread.currentThread().getName()+"被中断");
        }
    }
}